<template>
  <div class="goodsList">
    <!-- 面包屑 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索框 -->
    <el-input v-model="inputVal" placeholder="请输入内容" class="search-input" @keydown.native="handleSearch">
      <el-button slot="append" icon="el-icon-search" @click="handleSearch"></el-button>
    </el-input>
     <el-button type="primary" plain @click="addGoods">添加商品</el-button>
     <!-- 表格 -->
      <el-table
        border
        :data="goodsList"
        highlight-current-row
        style="width: 100%">
        <el-table-column
          type="index"
          width="50"
          label="编号"
          class="number">
        </el-table-column>
        <el-table-column
          prop="goods_name"
          label="商品名称"
          width="120">
        </el-table-column>
        <el-table-column
          property="goods_price"
          label="商品价格"
          width="120">
        </el-table-column>
        <el-table-column
          label="商品状态">
          <!-- 为了使用过滤器，这里使用插槽的写法 -->
          <template slot-scope="scope">
            <el-tag :type="getType(scope.row.goods_state)">{{scope.row.goods_state |ftmState}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          property="goods_weight"
          label="商品重量">
        </el-table-column>
        <el-table-column
          label="创建时间">
          <!-- 使用过滤器 -->
          <template slot-scope="scope">
            <div>{{scope.row.add_time |ftmTime}}</div>
          </template>
        </el-table-column>
        <el-table-column
          label="操作">
            <template slot-scope="scope">
              <el-button type="primary" icon="el-icon-edit" circle></el-button>
              <el-button type="danger" icon="el-icon-delete" circle @click="handleDelete(scope.row)"></el-button>
              <el-button type="success" icon="el-icon-check" circle></el-button>
            </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="1"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="100"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
  </div>
</template>
<script>
import { getGoodsList, deleteGoods } from '@/api'
export default {
  data () {
    return {
      inputVal: '',
      goodsList: [],
      pagenum: 1, // 默认为第一页
      pagesize: 10,
      total: 0
    }
  },
  // 过滤器
  filters: {
    // 状态
    ftmState (state) {
      if (state === '0') {
        return '未通过'
      } else if (state === '1') {
        return '审核中'
      } else {
        return '已审核'
      }
    },
    // 时间
    ftmTime (time) {
      var date = new Date(time)
      var y = date.getFullYear()
      var m = date.getMonth()
      var d = date.getDate()
      return y + '-' + m + '-' + d
    }
  },
  created () {
    this.initTable()
  },
  methods: {
    initTable () {
      getGoodsList({ query: this.inputVal, pagenum: this.pagenum, pagesize: this.pagesize }).then(res => {
        // console.log(res)
        this.goodsList = res.data.goods
        this.total = res.data.total
      })
    },
    // 状态颜色的改变
    getType (state) {
      if (state === '0') {
        return 'danger'
      } else if (state === '1') {
        return 'info'
      } else {
        return 'success'
      }
    },
    // 删除按钮
    handleDelete (row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteGoods(row.goods_id)
          .then(res => {
            // console.log(res)
            if (res.meta.status === 200) {
              this.$message(res.meta.msg)
              // 刷新页面
              this.initTable()
            } else {
              this.$message.error(res.meta.msg)
            }
          })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 搜索功能
    handleSearch () {
      this.initTable()
      // 清空搜索框
      this.inputVal = ''
    },
    // 分页
    handleSizeChange (val) {
      // console.log(`每页 ${val} 条`)
      // 每页多少条，传给pagesize
      this.pagesize = val
      // 刷新页面
      this.initTable()
    },
    handleCurrentChange (val) {
      // console.log(`当前页: ${val}`)
      // 当前页
      this.pagenum = val
      // 刷新页面
      this.initTable()
    },
    // 添加商品
    addGoods () {
      this.$router.push({ name: 'add' })
    }
  }
}
</script>
<style lang="scss" scoped>
.search-input{
  width: 300px;
  margin-top: 20px;
  margin-right: 5px;
  margin-bottom: 20px;
}
</style>
